

#include "mw-test-common.h"


void test_type_readwrite(gpointer item, 
			 mw_new_func newf, mw_free_func freef,
			 mw_write_func writef, mw_read_func readf, 
			 mw_equal_func eqf) {

  gpointer item_b;
  MwOpaque o =  { 0, 0 };
  MwReadBuffer *readb;
  MwWriteBuffer *writeb;
  GError *err = NULL;
  
  /* create a write buffer, and write the item to it, then finalize
     into opaque o */
  writeb = mw_write_buffer_new();
  writef(writeb, item);
  mw_write_buffer_finalize(writeb, &o);

  /* create a new item of the same type, then create a read buffer
     wrapping opaque o, and read onto the new item */
  item_b = newf();
  readb = mw_read_buffer_new_wrap(&o);
  readf(readb, item_b, &err);

  /* clean up the read buffer and the opaque */
  mw_read_buffer_free(readb);
  mw_opaque_clear(&o);

  /* assert that no error was raised, and that the original item is
     equivalent to the new item */
  g_assert(err == NULL);
  g_assert(eqf(item, item_b));
}



void test_compount_type(gpointer item,
			mw_new_func newf,
			mw_dup_func dupf,
			mw_copy_func copyf,
			mw_read_func readf,
			mw_write_func writef,
			mw_clear_func clearf,
			mw_free_func freef,
			mw_hash_func hashf,
			mw_equal_func eqf) {

  test_type_copy(item, newf, freef, copyf, eqf);
  test_type_dup(item, dupf, freef, eqf);
  test_type_readwrite(item, newf, freef, writef, readf, eqf);
  test_type_hash_equal(item, hashf, eqf);

}


